package com.springboot.demo.controller;

import com.springboot.demo.common.result.FrontJSONResult;
import com.springboot.demo.dto.ChbUserDto;
import lombok.extern.java.Log;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import java.io.FileWriter;

@RestController
@RequestMapping(value = "jackson")
@Log
public class JackSonTest {

    @RequestMapping(value = "/jktest",method = RequestMethod.GET)
    public FrontJSONResult jacksonTest(){
        log.info("insert....");
        ChbUserDto chbUser = new ChbUserDto();
        chbUser.setNickName("你猜");
        chbUser.setPhoneNumber("15586887542");
        return  FrontJSONResult.fromExecuteResult(chbUser);
    }

    public String thymeleafPage() throws Exception {

        // 构造模板引擎
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        resolver.setPrefix("templates/");
        resolver.setSuffix(".html");
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(resolver);

        // 构造上下文(Model)
        Context context = new Context();
        context.setVariable("name", "支付参数列表");
        context.setVariable("array", new String[]{"LS001235465", "32.5"});

        // 渲染模板
        FileWriter write = new FileWriter("result.html");
        // 模板名称、上下文内容
        templateEngine.process("payClient", context, write);

        String result = templateEngine.process("payClient", context);

        return result;
    }
}
